﻿namespace Moe.ECamel.Common.Entities
{
	using System;
	using System.ComponentModel;
	using System.Linq;

	partial class Subscription : IEditableObject
	{
		public String SubscriptionTypeName
		{
			get { return this.SubscriptionType.SubscriptionTypeName; }
		}
		public float TotalPrice
		{
			get { return this.SubscriptionItemTypeInfos.Sum(a => a.Price); }
		}
		public float TotalDeposit
		{
			get { return this.SubscriptionItemTypeInfos.Sum(a => a.Deposit); }
		}

		public ECamelError Validate()
		{
			return null;
		}

		public void InitializeNewSubscription(SubscriptionType subscriptionType, DateTime activationDate)
		{
			this.ActivationDate = activationDate;
			this.ExpirationDate = this.ActivationDate.AddMonths(subscriptionType.Duration);
			this.Active = true;
			this.SubscriptionType = subscriptionType;
			foreach (var info in subscriptionType.SubscriptionTypeItemTypeInfos)
			{
				var itemTypeInfo = new SubscriptionItemTypeInfo
				          	{Subscription = this, ItemTypeId = info.ItemTypeId, ItemType = info.ItemType, Quantity = 0};
				this.SubscriptionItemTypeInfos.Add(itemTypeInfo);
			}
		}

		#region IEditableObject Members

		private Subscription _originalEntity = null;

		public void BeginEdit()
		{
			if (this._originalEntity == null)
			{
				// Initialize original
				this._originalEntity = new Subscription();
				// Copy editable properties values:
				this._originalEntity._Active = this._Active;
			}
		}

		public void CancelEdit()
		{
			if (this._originalEntity != null)
			{
				// Restore properties:
				this._Active = this._originalEntity._Active;
				// Reset original values:
				this._originalEntity = null;
			}
		}

		public void EndEdit()
		{
			// Reset original values:
			this._originalEntity = null;
		}

		#endregion
	}
}
